import {
  getCategoryList, searchProduct
} from '../../api/api.js';

const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    categories: [],
    productList: [],
    isShow: true,//当前最大页数
    navActive: 0,
    page: {
      pageSize: 10,
      pageNum: 1
    },
    parameter: {
      'navbar': '1',
      'return': '0',
      'title': '产品分类'
    },
    navH: "",
    number: "",
    height: ""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (res) {
    this.getAllCategory();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  getAllCategory: function () {
    var that = this;
    getCategoryList().then(res => {
      console.log(res);
      that.setData({
        categories: res.data
      });
      that.infoScroll();
    }).catch(err => {
      app.Tips({
        title: err
      })
    })
  },
  infoScroll: function () {
    var that = this;
    var len = that.data.categories.length;
    that.setData({
      navH: app.globalData.navHeight
    })
    //设置商品列表高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: (res.windowHeight) * (750 / res.windowWidth) - 98 - app.globalData.navHeight
          //res.windowHeight:获取整个窗口高度为px，*2为rpx；98为头部占据的高度；
        })
      },
    });
    //获取默认商品数据
    this.fetchProductList();
  },
  tap: function (e) {
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    this.setData({
      navActive: index
    });
    this.fetchProductList();
  },
  scroll: function (e) {
    var scrollTop = e.detail.scrollTop;
    for (var i = 0; i < scrollArr.length; i++) {
      if (scrollTop >= 0 && scrollTop < scrollArr[1] - scrollArr[0]) {
        this.setData({
          navActive: 0,
          lastActive: 0
        })
      } else if (scrollTop >= scrollArr[i] - scrollArr[0] && scrollTop < scrollArr[i + 1] - scrollArr[0]) {
        console.log(scrollArr[1] - scrollArr[0])
        this.setData({
          navActive: i
        })
      } else if (scrollTop >= scrollArr[scrollArr.length - 1] - scrollArr[0]) {
        this.setData({
          navActive: scrollArr.length - 1
        })
      }
    }
  },
  /**
   * 获取商品列表
   */
  fetchProductList() {
    //获取当前分类ID
    var category = this.data.categories[this.data.navActive];
    var cateIdList = [];
    cateIdList.push(category.id);
    //组装请求
    var param = {};
    param.page = this.data.page;
    param.condition = {
      cateIdList: cateIdList
    };
    var that = this;
    searchProduct(param).then(res => {
      that.setData({ productList: res.data });
      that.isShow(res.page);
    })
  },
  isShow(page) {
    var isShow = page.ppageNum >= page.pages;
    this.setData({ isShow: isShow });
  },
  searchSubmitValue: function (e) {
    var that = this;
    if (e.detail.value.length > 0)
      wx.navigateTo({
        url: '/packageB/goods_list/goods_list?searchValue=' + e.detail.value
      })
    else
      return app.Tips({
        title: '请填写要搜索的产品信息'
      });
  }
})